package com.hualan.service.shop;

import com.alibaba.fastjson.JSON;
import com.hualan.entity.Shops;
import com.hualan.utils.EntityFactory;
import com.hualan.utils.ExecUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = "/shop")
public class ShopService extends HttpServlet {
    private List<Shops> shopsList;

    @Override
    public void init() throws ServletException {
        this.shopsList = new ArrayList<>();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.shopsList.clear();
        //获取请求网址
        if(!req.getServletPath().equals("/shop")){
            resp.getWriter().write("403 not allowed");
            return;
        }
        //定义sql语句完成数据库指定的操作
        //?是一个占位符，代表当前位置存在一个外界的变量数据
        String sql = "";
        List<Object> params = new ArrayList<>();
        if (req.getParameter("phone") == null && req.getParameter("shopName") == null && req.getParameter("shopStatus") == null && req.getParameter("startTime") == null && req.getParameter("endTime") == null && req.getParameter("pageName") != null) {

            sql = "select * from shop";
//            params.add(req.getParameter("pageName"));

        } else if (req.getParameter("phone") != null && req.getParameter("shopName") == null && req.getParameter("shopStatus") == null && req.getParameter("startTime") == null && req.getParameter("endTime") == null && req.getParameter("pageName") != null) {

            sql = "select * from shop where shopPhone like ?";
            params = new ArrayList<>();
            params.add("%" + req.getParameter("phone") + "%");
            System.out.println("%" + req.getParameter("phone") + "%");

        } else if (req.getParameter("phone") != null && req.getParameter("shopName") != null && req.getParameter("shopStatus") == null && req.getParameter("startTime") == null && req.getParameter("endTime") == null && req.getParameter("pageName") != null) {

            sql = "select * from shop where shopName like ? and shopPhone like ?";
            params = new ArrayList<>();
            params.add("%" + req.getParameter("shopName") + "%");
            params.add("%" + req.getParameter("phone") + "%");

        } else if (req.getParameter("phone") != null && req.getParameter("shopName") != null && req.getParameter("shopStatus") != null && req.getParameter("startTime") == null && req.getParameter("endTime") == null && req.getParameter("pageName") != null) {

            sql = "select * from shop where shopStatus = ? and shopName like ? and shopPhone like ?";
            params = new ArrayList<>();
            params.add(req.getParameter("shopStatus"));
            params.add("%" + req.getParameter("shopName") + "%");
            params.add("%" + req.getParameter("phone") + "%");
        }
        ///////////////////////////////////////////////////
        try {
            ResultSet resultSet = (ResultSet) ExecUtil.execute(sql, params, req, false);
            //4.提取结果集中的数据
            while (resultSet.next()) {
                EntityFactory entityFactory = new EntityFactory(Shops.class);
                Shops shops = (Shops) entityFactory.startCombine(resultSet);
                this.shopsList.add(shops);
            }
            int from = (Integer.parseInt(req.getParameter("pageName")) - 1) * 10;
            int to = from + 11;
            if (to > shopsList.size()) {
                if (from <= to) {
                    resp.getWriter().write(JSON.toJSONString(this.shopsList.subList(from, shopsList.size())));
                }
            } else {
                resp.getWriter().write(JSON.toJSONString(this.shopsList.subList(from, to)));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //添加
        if(!req.getServletPath().equals("/shop")){
            resp.getWriter().write("403 not allowed");
            return;
        }
        //定义sql语句完成数据库指定的操作
        //?是一个占位符，代表当前位置存在一个外界的变量数据
        String sql = "insert into shop(shopName, shopImage, shopPhone, shopProvince, shopCity, shopDistrict, shopAddress, shopLongitude, shopLatitude, shopDescribe) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        List<Object> params = new ArrayList<>();
        params.add(req.getParameter("shopName"));
        params.add(req.getParameter("shopImage"));
        params.add(req.getParameter("shopPhone"));
        params.add(req.getParameter("shopProvince"));
        params.add(req.getParameter("shopCity"));
        params.add(req.getParameter("shopDistrict"));
        params.add(req.getParameter("shopAddress"));
        params.add(req.getParameter("shopLongitude"));
        params.add(req.getParameter("shopLatitude"));
        params.add(req.getParameter("shopDescribe"));
        try {
            boolean result = (Boolean) ExecUtil.execute(sql, params, req, true);
            //4.提取结果集中的数据
            if (result == true) {
                resp.getWriter().write(JSON.toJSONString("add success"));
                ExecUtil.commit();
            } else {
                resp.getWriter().write(JSON.toJSONString("fail"));

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if(!req.getServletPath().equals("/shop")){
            resp.getWriter().write("403 not allowed");
            return;
        }
        String sql = "delete from shop where shopId=?";
        List<Object> params = new ArrayList<>();
        params.add(req.getParameter("shopId"));
        try {
            boolean result = (Boolean) ExecUtil.execute(sql, params, req, true);
            //4.提取结果集中的数据
            if (result) {
                resp.getWriter().write(JSON.toJSONString("delete success"));
                ExecUtil.commit();
            } else {
                resp.getWriter().write(JSON.toJSONString("fail"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //更新
        if(!req.getServletPath().equals("/shop")){
            resp.getWriter().write("403 not allowed");
            return;
        }
        //定义sql语句完成数据库指定的操作
        //?是一个占位符，代表当前位置存在一个外界的变量数据
        String sql = "update shop set shopName=?, shopImage=?, shopPhone=?, shopProvince=?, shopCity=?, shopDistrict=?, shopAddress=?, shopLongitude=?, shopLatitude=?, shopDescribe=? where shopId=?";
        List<Object> params = new ArrayList<>();
        params.add(req.getParameter("shopName"));
        params.add(req.getParameter("shopImage"));
        params.add(req.getParameter("shopPhone"));
        params.add(req.getParameter("shopProvince"));
        params.add(req.getParameter("shopCity"));
        params.add(req.getParameter("shopDistrict"));
        params.add(req.getParameter("shopAddress"));
        params.add(req.getParameter("shopLongitude"));
        params.add(req.getParameter("shopLatitude"));
        params.add(req.getParameter("shopDescribe"));
        params.add(req.getParameter("shopId"));

        try {
            boolean result = (Boolean) ExecUtil.execute(sql, params, req, true);
            //4.提取结果集中的数据
            if (result == true) {
                resp.getWriter().write(JSON.toJSONString("update success"));
                ExecUtil.commit();
            } else {
                resp.getWriter().write(JSON.toJSONString("fail"));

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
